<?php

namespace App\Controller\Home;

use App\Controller\Example\FundsExample;
use App\Models\Conn;
use App\Models\FundsModel;
use App\Models\UsersModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Enums\FundsGenreEnum;
use Core\Result\Result;

class Funds
{

    /**
     * 流水查询
     * entry  token genre page limit
     * return json
     * */
    public function getFundsList($data){
        $assemble = Common::assemble(new FundsExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $assemble->uid = UsersModel::getUserInside($conn, $assemble->token)["id"];
        if(!in_array($assemble->genre,array(1,2,5))){
            Result::error(ErrorEnum::RUNNINGWATERSTATUSERROR);
        }
        $rst["list"] = FundsModel::getFunds($conn, $assemble, $identical);
        $rst["count"] = FundsModel::getFundsCount($conn, $assemble);
        Result::success($rst);
    }
    /**
     * 写入流水
     * entry  data
     * return int
     * */
    public static function writeFunds($conn,$data){
        $fundsExample = Common::assemble(new FundsExample(), $data);
        return FundsModel::insertFunds($conn,$fundsExample);
    }

    /**
     * 写入流水(余额)
     * entry `uid`, `number`, `type`, `amount`, `balance`, `notes`
     * return int
     * */
    public static function writeFundsMoney($conn,$uid,$number,$type,$amount,$balance,$notes){
        return self::writeFundsCurrency($conn,$uid,$number,$type,$amount,$balance,$notes,FundsGenreEnum::MONEY);
    }
    /**
     * 写入流水(积分)
     * entry `uid`, `number`, `type`, `amount`, `balance`, `notes`
     * return int
     * */
    public static function writeFundsIntegral($conn,$uid,$number,$type,$amount,$balance,$notes){
        return self::writeFundsCurrency($conn,$uid,$number,$type,$amount,$balance,$notes,FundsGenreEnum::TINYMONEY);
    }
    /**
     * 写入流水(贡献值)
     * entry `uid`, `number`, `type`, `amount`, `balance`, `notes`
     * return int
     * */
    public static function writeFundsTinyMoney($conn,$uid,$number,$type,$amount,$balance,$notes){
        return self::writeFundsCurrency($conn,$uid,$number,$type,$amount,$balance,$notes,FundsGenreEnum::VENTUREMONEY);
    }
    /**
     * 写入流水(通用)
     * entry `uid`, `number`, `type`, `amount`, `balance`, `notes`,`genre`
     * return int
     * */
    public static function writeFundsCurrency($conn,$uid,$number,$type,$amount,$balance,$notes,$genre){
        $fundsExample = new FundsExample();
        $fundsExample->uid = $uid;
        $fundsExample->number = $number;
        $fundsExample->type = $type;
        $fundsExample->amount = $amount;
        $fundsExample->balance = $balance;
        $fundsExample->notes = $notes;
        $fundsExample->genre = $genre;
        return FundsModel::insertFunds($conn,$fundsExample);
    }
}